//customized Panel used to display information messages
// messages are displayed via a simple Label that will get cut off if they are too long
// using a Label will allow messages formatted in HTML to be viewed
// new messages can be appended to existing messages
// timers are linked to new messages to help determine when a message should be cycled out

//import only what we need
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.Timer;
import java.awt.Graphics;
import java.awt.Font;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

public class PopupLayer extends JPanel implements ActionListener {
	
	//declare some class objects
	private JLabel messenger;
	private ArrayList<String> messages;
	private ArrayList<Timer> tickers;
	
	//our constructor
	public PopupLayer(Rectangle bounds) {
		
		//call ancestor
		super();
		
		//initialize the Label
		// define a default font and color in case the message has no HTML formatting
		this.messenger = new JLabel();
		//this.messenger.setText("Default Message");
		this.messenger.setFont(new Font("Courier New", Font.BOLD, 8));
		this.messenger.setForeground(Color.BLACK);
		
		//set properties for this layer
		this.messages = new ArrayList<String>();
		this.tickers = new ArrayList<Timer>();
		this.setLayout(new GridLayout(1,1));
		this.setBackground(new Color(0, 80, 200, 200));
		this.setVisible(true);
		this.setOpaque(true);
		if (bounds != null){
			this.setBounds(bounds);
		}else{
			System.out.println("POPUPLAYER: bounds were null!");
		}
		this.add(this.messenger);
		
	}
	
	//override painting method
	public void paintComponent(Graphics g) {
		
		//call ancestor
		super.paintComponent(g);
		
		//System.out.println("$1PopupLayer: painting...");
		
	}
	
	//method to verify that there are more messages to be displayed
	public boolean hasMessage() {
		
		if (this.messages != null){
			return !this.messages.isEmpty();
		}else{
			return false;
		}
		
	}
	
	//method to append the specified message to the end of the current message
	// and setup the timer that goes along with it for the specified time delay
	public void appendMessage(String message, double delayseconds) {
		
		System.out.println("$2<div style=\"color:red\">Popup: appending message = " + message + " ->- for seconds = " + delayseconds + "</div>");
		//add the new text to the end of the Label's current text
		this.messenger.setText(this.messenger.getText() + message);
		//add message to the list
		this.messages.add(message);
		//build and add a timer to the list
		Timer removaltimer = new Timer((int)Math.ceil(delayseconds * 1000), this);
		this.tickers.add(removaltimer);
		this.resetMessages();
		removaltimer.start();
		
	}
	
	//hidden convenience method for adding the existing messages to the messenger
	private void resetMessages() {
		
		String fullmessage = "<html>";
		//add up all the messages
		for (int m=0; m<this.messages.size(); ++m){
			fullmessage = fullmessage + this.messages.get(m) + "<br>";
		}
		fullmessage = fullmessage + "</html>";
		//set the message
		this.messenger.setText(fullmessage);
		//ensure changes are shown
		//this.setBounds(this.getBounds());
		this.validate();
		this.repaint();
		//this next part is an unwanted hack, but the parent component refuses to repaint
		UIController.progframe.validate();
		UIController.progframe.repaint();
		
	}
	
	//method overridden to handle timer events
	public void actionPerformed(ActionEvent EVE) {
		
		System.out.println("Popup: event fired");
		//find out who fired the event
		whofired: for (int t=0; t<this.tickers.size(); ++t){
			if (EVE.getSource().equals(this.tickers.get(t))){
				System.out.println("Popup: timer = " + this.tickers.get(t) + " ->  message = " + this.messages.get(t));
				//stop and remove the timer
				this.tickers.get(t).stop();
				this.tickers.remove(t);
				//remove the relating message and reset the messenger text
				this.messages.remove(t);
				this.resetMessages();
				break whofired;
			}
		}
		
	}
	
}